package org.weed.remote.web;

import java.awt.AWTException;
import java.awt.Robot;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.Enumeration;

import org.weed.remote.service.KeyBoardService;
import org.weed.remote.service.MouseService;

public class RemoteServer {
	private int port;
	private Robot robot;
	private KeyBoardService keyboard;
	private ServerSocket server;
	
	public RemoteServer(int port) throws AWTException {
		this.port = port;
		robot = new Robot();
		keyboard = new KeyBoardService(robot);
	}
	
	public void run() {
		try {
			server = new ServerSocket(port);
			System.err.println("Server starts at " + getInetAddress() + ":" + port);
			while (true) {
				Socket client = server.accept();
				System.out.println(client.getRemoteSocketAddress() + " connected.");
				InputStream inputStream = client.getInputStream();
				
				BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
				
				//mouseService.start();
				String request;
				while ((request = reader.readLine()) != null) {
					System.out.println(request);
					doRequest(request);
				
					
				}
				
				System.out.println(client.getRemoteSocketAddress() + " disconnected.");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Unable to listen at port " + port);
		}
	}
	
	public void stop() {
		try {
			server.close();
		} catch (Exception e) {
			
		}
	}
	
	protected void doRequest(String request) {
		String type = request.substring(0, 1);
		if ("K".equals(type)) {
			keyboard.keyClick(request.substring(1));
		} else if ("M".equals(type)) {
//			if (mouseService.isRunning()) {
//				mouseService.pause();
//			}
//			int end = request.indexOf("y");
//			int x = ((int) Float.parseFloat(request.substring(2, end)));
//			int y = (int) Float.parseFloat(request.substring(end+1));
//			mouseService.mouseMove(x, y);
		} else if ("C".equals(type)) {
//			if ("R".equals(request.substring(1))) {
//				mouseService.mouseClick(InputEvent.BUTTON3_DOWN_MASK);
//			} else {
//				mouseService.mouseClick(InputEvent.BUTTON1_DOWN_MASK);
//			}
		} else if ("S".equals(type)) {
//			if (!mouseService.isRunning()) {
//				mouseService.start();
//			}
//			int end = request.indexOf("y");
//			int x = ((int) Double.parseDouble(request.substring(2, end)));
//			int y = (int) Double.parseDouble(request.substring(end+1));
//			mouseService.setSpeed(-1*x, y);
		}
	}
	
	public InetAddress getInetAddress() {
		try {
			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
			for (NetworkInterface net : Collections.list(interfaces)) {			
				Enumeration<InetAddress> inetAddresses = net.getInetAddresses();
				for (InetAddress inetAddress : Collections.list(inetAddresses)) {
					if (("" + inetAddress).startsWith("/192.168.1.")) {
						return inetAddress;
					}
				}
			}
		} catch (Exception e) {
			
		}
		return null;
	}
	
	public static void main(String[] args) throws Exception {		
		int port = 8888;		
		RemoteServer server = new RemoteServer(port);
		server.run();
	}
}
